package com.qiaoyanqing.textdemoqyq7.Activities;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qiaoyanqing.textdemoqyq7.Adapters.HisAdapter;
import com.qiaoyanqing.textdemoqyq7.Beans.Baike;
import com.qiaoyanqing.textdemoqyq7.R;
import com.qiaoyanqing.textdemoqyq7.SQL.MyCollegeHelper;

import java.util.ArrayList;
import java.util.List;

public class ShoucangActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView mListView;
    private List<Baike> mData=new ArrayList<>();
    private HisAdapter mAdapter;

    private MyCollegeHelper mHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoucang);
        mListView= (ListView) findViewById(R.id.shoucang_listview);
        mHelper=new MyCollegeHelper(this);

        initData();

        initListview();
        //设置条目点击事件
        initItemClickListener();


    }

    private void initItemClickListener() {
        mListView.setOnItemClickListener(this);
    }

    private void initListview() {
        mAdapter=new HisAdapter(this,mData);
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        SQLiteDatabase db=mHelper.getWritableDatabase();
        Cursor cursor=db.query(true,"shoucang",null,null,null,null,null,null,null);
        while (cursor.moveToNext()) {
            String  id=cursor.getString(cursor.getColumnIndex("id"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String source=cursor.getString(cursor.getColumnIndex("source"));
            String create_time=cursor.getString(cursor.getColumnIndex("create_time"));
            Baike baike=new Baike();
            baike.setTitle(title);
            baike.setSource(source);
            baike.setId(id);
            baike.setCreate_time(create_time);
            mData.add(baike);
        }
    }


    public void backMA(View view) {
        Intent intent=new Intent(ShoucangActivity.this,CollectionActivity.class);
        startActivity(intent);
        finish();
    }

    public void homeclick(View view) {
        Intent intent=new Intent(ShoucangActivity.this,CollectionActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        Intent intent=new Intent(ShoucangActivity.this,WebViewActivity.class);
        intent.putExtra("page",mData.get(0).getId());
        startActivity(intent);

    }
}
